package com.stud.common.core.domain;

import com.stud.common.constant.HttpStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应对象
 *
 * @author Stud
 */
@Schema(description = "响应对象")
@Data
public class Result<T> implements Serializable {
    /**
     * 成功
     */
    private static final int SUCCESS = HttpStatus.SUCCESS;

    /**
     * 失败
     */
    private static final int FAIL = HttpStatus.ERROR;

    @Schema(description = "响应状态")
    private boolean success;

    @Schema(description = "响应码")
    private int code;

    @Schema(description = "响应消息")
    private String msg;

    @Schema(description = "响应数据")
    private T data;

    public static <T> Result<T> ok(int num) {
        if (num == 0) {
            return restResult(false, null, FAIL, "操作失败");
        }
        return restResult(true, null, SUCCESS, "操作成功");
    }

    public static <T> Result<T> ok(boolean oj8k) {
        if (oj8k) {
            return restResult(true, null, SUCCESS, "操作成功");
        }
        return restResult(false, null, FAIL, "操作失败");
    }

    public static <T> Result<T> ok() {
        return restResult(true, null, SUCCESS, "操作成功");
    }

    public static <T> Result<T> ok(T data) {
        return restResult(true, data, SUCCESS, "操作成功");
    }

    public static <T> Result<T> ok(String msg, T data) {
        return restResult(true, data, SUCCESS, msg);
    }

    public static <T> Result<T> fail() {
        return restResult(false, null, FAIL, "操作失败");
    }

    public static <T> Result<T> fail(String msg) {
        return restResult(false, null, FAIL, msg);
    }

    public static <T> Result<T> fail(T data) {
        return restResult(false, data, FAIL, "操作失败");
    }

    public static <T> Result<T> fail(String msg, T data) {
        return restResult(false, data, FAIL, msg);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return restResult(false, null, code, msg);
    }

    private static <T> Result<T> restResult(boolean success, T data, int code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setSuccess(success);
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

}
